package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.DishFlavorDao;
import com.example.dto.DishDto;
import com.example.entity.Dish;
import com.example.dao.DishDao;
import com.example.entity.DishFlavor;
import com.example.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理(Dish)表服务实现类
 *
 * @author makejava
 * @since 2022-05-23 13:55:18
 */
@Service("dishService")
public class DishServiceImpl extends ServiceImpl<DishDao,Dish> implements DishService {
    @Autowired
    private DishDao dishDao;
    @Autowired
    private DishFlavorServiceImpl dishFlavorService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Dish queryById(Long id) {
        return this.dishDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Dish> queryAllByLimit(int offset, int limit) {
        return this.dishDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param dish 实例对象
     * @return 实例对象
     */
    @Override
    public Dish insert(Dish dish) {
        this.dishDao.insert(dish);
        return dish;
    }

    /**
     * 修改数据
     *
     * @param dish 实例对象
     * @return 实例对象
     */
    @Override
    public Dish update(Dish dish) {
        this.dishDao.update(dish);
        return this.queryById(dish.getId());
    }

    @Override
    public DishDto getDishDtoById(Long id) {
        Dish dish=this.getById(id);
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);
        return dishDto;


    }

    @Transactional
    @Override
    public int saveWithFlavor(DishDto dishDto) {
        boolean a=this.save(dishDto);
        Long dishid=dishDto.getId();
        List<DishFlavor>flavors=dishDto.getFlavors();
        flavors.stream().map((item)->{
            item.setDishId(dishid);
            item.setIsDeleted(0);
            return item;
        }).collect(Collectors.toList());
        boolean b=dishFlavorService.saveBatch(flavors);
        if(a&&b){
            return 1;
        }else{
            return 0;
        }

    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.dishDao.deleteById(id) > 0;
    }
    @Transactional
    @Override
    public int updateNewDish(DishDto dishDto) {
        Long dishid=dishDto.getId();
        boolean a=this.updateById(dishDto);
        List<DishFlavor>list=dishDto.getFlavors();
        LambdaQueryWrapper<DishFlavor>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishid);
        for (int i = 0; i <list.size() ; i++) {
            list.get(i).setDishId(dishid);
            list.get(i).setIsDeleted(0);
        }
        dishFlavorService.remove(queryWrapper);
        boolean b=dishFlavorService.saveBatch(list);
        if(a&&b){
           return 1;
        }else{
            return 2;
        }
    }

    @Override
    public void updateIsdelete(Long id) {
        dishDao.updateisdelete(id);
    }

    @Override
    public void updatethisstatus(Long id) {
        dishDao.updatestatus(id);
        return;
    }

}